export enum ApiResultCode {
    Success = 200,
    NotFound = 404,
    InternalServerError = 500,

    // 通用逻辑错误码 从1000开始
    InvalidParameters = 1001,
    Unauthorized = 1002,

    // 业务逻辑错误码 从10000开始 例如 11000 - 11999 为用户模块
    UserNotFound = 11001,
}


export interface IApiRequestData { }
export interface IApiRequest {
    m: string;                  // module name
    f: string;                  // function name
    d?: IApiRequestData;        // data
}

export interface IApiResponseData { }
export interface IApiResponse<T extends IApiResponseData = IApiResponseData> {
    r: number;                       // result code
    m?: string;                      // message    
    d?: T;            // data
}

export class ApiResponse<T extends IApiResponseData = IApiResponseData> implements IApiResponse {
    r: number = ApiResultCode.InternalServerError;
    m?: string;
    d?: T;

    constructor(code: ApiResultCode | number, message?: string, data?: T) {
        this.r = code;
        this.m = message;
        this.d = data;
    }

    static create<T extends IApiResponseData = IApiResponseData>(code: ApiResultCode | number, message?: string, data?: T): IApiResponse<T> {
        return new ApiResponse(code, message, data);
    }

    static success<T extends IApiResponseData = IApiResponseData>(data: T): IApiResponse<T> {
        return new ApiResponse(ApiResultCode.Success, undefined, data);
    }

    static fail<T extends IApiResponseData = never>(code: ApiResultCode | number, message?: string): IApiResponse<T> {
        return new ApiResponse(code, message);
    }

}